package com.bysc.cmsservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bysc.cmsservice.entity.CmsAnnouncement;
import com.bysc.cmsservice.service.CmsAnnouncementService;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.commonutils.utils.JwtUtils;
import com.bysc.commonutils.vo.BaseQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 公告 前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-10-22
 */
@RestController
@RequestMapping("/cmsservice/cms-announcement")
@Api(tags = "公告管理")
@Slf4j
public class CmsAnnouncementController {

    @Autowired
    private CmsAnnouncementService cmsAnnouncementService;


    /*
    * 新增字段clubId
    * */
    @ApiOperation("添加公告(仅后台)（没测试！！！ 可能会有问题）")
    @PostMapping("addAnnouncement")
    public Results addAnnouncement(@RequestBody CmsAnnouncement announcement, HttpServletRequest request) {
        String TOKEN_clubId = JwtUtils.getClubIdByJwtToken(request);
        if(!StringUtils.isEmpty(TOKEN_clubId)){
            announcement.setClubId(TOKEN_clubId);
        }
        cmsAnnouncementService.save(announcement);
        return Results.ok().data("announcement", announcement).message(MessageConstant.ADD_ANNOUNCEMENT_SUCCESS);
    }

    @ApiOperation("更新公告（后台的后端要传clubId）")
    @PostMapping("updateAnnouncement")
    public Results updateAnnouncement(@RequestBody CmsAnnouncement announcement) {
        cmsAnnouncementService.updateById(announcement);
        return Results.ok().data("announcement", announcement).message(MessageConstant.UPDATE_ANNOUNCEMENT_SUCCESS);
    }


    @ApiOperation("根据id查找公告")
    @GetMapping("getAnnouncementById/{id}")
    public Results getAnnouncementById(@PathVariable String id) {
        CmsAnnouncement announcement = cmsAnnouncementService.getById(id);
        return Results.ok().data("announcement", announcement).message(MessageConstant.QUERY_ANNOUNCEMENT_SUCCESS);
    }

    /*
    *
    * 新增根据clubId查询公告列表
    * */
    @ApiOperation("分页查询公告(前台后台通用)")
    @PostMapping("getAnnouncementList/{page}/{limit}")
    public Results getAnnouncementList(
            @ApiParam(name = "page", value = "查询页数", required = true) @PathVariable Integer page,
            @ApiParam(name = "limit", value = "查询数量", required = true) @PathVariable Integer limit,
            @RequestBody(required = false) BaseQueryVo queryVo, HttpServletRequest request) {

        QueryWrapper<CmsAnnouncement> wrapper = new QueryWrapper<>();
        if (queryVo != null) {
            //获取announcement中所有属性值
            String id = queryVo.getId();
            String value = queryVo.getValue();
            String clubId = queryVo.getClubId();
            if (!StringUtils.isEmpty(clubId)) {
                wrapper.eq("club_id", clubId);
            }
            else{
                String TOKEN_clubId = JwtUtils.getClubIdByJwtToken(request);
                if(!StringUtils.isEmpty(TOKEN_clubId)){
                    wrapper.eq("club_id", TOKEN_clubId);
                }
            }
            if (!StringUtils.isEmpty(id)) {
                wrapper.eq("id", id);
            }
            if (!StringUtils.isEmpty(value)) {
                wrapper.like("title", value).or().like("content", value);
            }
        }
        wrapper.orderByDesc("is_top");
        wrapper.orderByDesc("gmt_create");
        IPage<CmsAnnouncement> page1 = new Page<>();
        Page<CmsAnnouncement> pageParam = new Page<>(page, limit);
        page1 = cmsAnnouncementService.page(pageParam, wrapper);
        return Results.ok().data("list", page1).message(MessageConstant.QUERY_ANNOUNCEMENT_SUCCESS);
    }

    @ApiOperation("根据id删除公告")
    @DeleteMapping("deleteAnnouncementById/{id}")
    public Results deleteAnnouncementById(@PathVariable String id) {
        cmsAnnouncementService.removeById(id);
        return Results.ok().message(MessageConstant.DELETE_ANNOUNCEMENT_SUCCESS);
    }
}
